﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Runtime.Intrinsics.Arm;

namespace RedistTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //github https://github.com/2881099/csredis

            var con = "192.168.0.248:6379,password=123456,defaultDatabase=10,ssl=false,writeBuffer=10240,poolsize=50,prefix=RedisTest:";

            //配置CSRedis
            var rs = new CSRedis.CSRedisClient(con);
            RedisHelper.Initialization(rs);
            var rds = RedisHelper.Instance;


            RedisHelper.Set("MyKey", new MyObject());

            var data = RedisHelper.Get("MyKey");

            Console.WriteLine(JsonConvert.SerializeObject(data));


            RedisHelper.HSet("h1", "id", 10);
            RedisHelper.HSet("h1", "name", "aaa");
            RedisHelper.HSet("h1", "dt", DateTime.Now);


            var dt = RedisHelper.HGet("h1", "dt");
            Console.WriteLine($"dt:{dt}");

            //Redis实现的简单消息队列 
            //sub1, sub2 争抢订阅（只可一端收到消息）
            var sub1 = RedisHelper.SubscribeList("list1", msg => Console.WriteLine($"sub1 -> list1 : {msg}"));
            var sub2 = RedisHelper.SubscribeList("list1", msg => Console.WriteLine($"sub2 -> list1 : {msg}"));

            //sub3, sub4, sub5 非争抢订阅（多端都可收到消息）
            var sub3 = RedisHelper.SubscribeListBroadcast("list2", "sub3", msg => Console.WriteLine($"sub3 -> list2 : {msg}"));
            var sub4 = RedisHelper.SubscribeListBroadcast("list2", "sub4", msg => Console.WriteLine($"sub4 -> list2 : {msg}"));
            var sub5 = RedisHelper.SubscribeListBroadcast("list2", "sub5", msg => Console.WriteLine($"sub5 -> list2 : {msg}"));

            //发布消息
            RedisHelper.RPush("list1", "测试消息1-1");
            RedisHelper.RPush("list1", "测试消息1-2");
            RedisHelper.RPush("list1", "测试消息1-3");
            RedisHelper.RPush("list1", "测试消息1-4");

            RedisHelper.RPush("list2", "测试消息2-1");
            RedisHelper.RPush("list2", "测试消息2-2");
            RedisHelper.RPush("list2", "测试消息2-3");
            RedisHelper.RPush("list2", "测试消息2-4");
            RedisHelper.RPush("list2", "测试消息2-5");


            //Redis5 Stram
            var streamKey1 = "StreamList-01";
            var streamKey2 = "StreamList-02";
            //发布消息
            var id1 = rds.XAdd(streamKey1, ("f1", "v1"));
            var id12 = rds.XAdd(streamKey1, ("f1", "v2"));

            var id2 = rds.XAdd(streamKey2, ("f2", "v1"));
            var id22 = rds.XAdd(streamKey2, ("f2", "v2"));

            //读取消息
            //如果读取开始的消息ID为0，表示从开关读起
            //在阻塞模式中，可以使用$，表示最新的消息ID
            var items1 = rds.XRead(10, 1000, (streamKey1, "0"));
            var items2 = rds.XRead(10, 1000, (streamKey2, "0"));

            //标记消息已消费,如果不标记已消费，消息会一直在队列中
            foreach(var item in items1)
            {
                foreach (var d in item.data)
                {
                    var id = d.id;
                    //rds.XAck(streamKey1, "", id);
                    //使用Xdel来标记消息已消费
                    rds.XDel(streamKey1, id);
                }
            }


        }
    }


    public class MyObject {
        public int id = 10;
public string name = "aaa";

        public DateTime dt = DateTime.Now;
}


}
